package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.RedisConst;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckitemService;
import com.itheima.service.SetMealService;
import com.itheima.utils.JedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import com.fasterxml.jackson.core.type.TypeReference;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author : wangjie
* @CreateDate : 2020/3/31 14:59
* @Version : 1.0
* @Description : 检查套餐表现层接口实现类
*/
@Service
@Transactional
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    //依赖注入setmealservice
    @Autowired
    private SetMealDao setMealDao;
    //依赖注入redis数据库
    @Autowired
    private JedisPool jedisPool;
    //依赖注入checkgroupservice
    @Autowired
    private CheckGroupService checkGroupService;
    //依赖注入checkitemservice
    @Autowired
    private CheckitemService checkitemService;

    /**
     * 套餐列表分页
     * @param pageBean
     * @return
     */
    @Override
    public PageResult queryPage(QueryPageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(),pageBean.getPageSize());
        Page<Setmeal> page= setMealDao.findByCondition(pageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 添加检查套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.debug("add:{}",setmeal,checkgroupIds);
        //调用dao 方法存储套餐基本数据
        setMealDao.add(setmeal);
        //调用dao 方法存储勾选的检查组ID至 t_setmeal_checkgroup
        for (Integer checkgroupId : checkgroupIds) {
            Map<String,Integer> map =new HashMap<>();
            map.put("setmeal_id",setmeal.getId());
            map.put("checkgroup_id",checkgroupId);
            setMealDao.addSetMealAndCheckGroup(map);
        }
        //保存图片名称到redis数据库
        Jedis jedis = jedisPool.getResource();
        jedis.sadd(RedisConst.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    /**
     * 删除套餐
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        setMealDao.deleteSetmealAndCheckGroupById(id);
        setMealDao.deleteById(id);
    }

    /**
     * 移动端 获取所有套餐信息
     * @return
     */
    @Override
    public List<Setmeal> getSetmeal() {
        List<Setmeal>setmealList=null;
        Jedis jedis = JedisUtils.getJeis();
        String setmeal = jedis.get("setmeal");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (setmeal==null){
            setmealList= setMealDao.findAll();
                String json = objectMapper.writeValueAsString(setmealList);
                jedis.set("setmeal",json);


            }else {
                setmealList=objectMapper.readValue(setmeal,new  TypeReference<List<Setmeal>>(){});
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return setmealList;
    }

    /**
     * 移动端 基于套餐ID获取套餐信息
     * @param setmealId
     * @return
     */
    @Override
    public Setmeal findById(Integer setmealId) {
        Setmeal setmeal= setMealDao.findById(setmealId);
        //基于套餐ID(先获取中间表数据 然后是检查组数据) 获取所有的检查组
        List<CheckGroup> checkGroups= checkGroupService.findCheckgroupById(setmeal.getId());
        setmeal.setCheckGroups(checkGroups);
        for (CheckGroup checkGroup : checkGroups) {
            List<CheckItem> checkItems= checkitemService.findCheckitemById(checkGroup.getId());
            checkGroup.setCheckItems(checkItems);
        }
        return setmeal;
    }
}
